package cn.exrick.xboot.common.timeTask.approval;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.common.utils.SpringUtils;
import cn.exrick.xboot.modules.ApprovalInfo.entity.ApprovalInfo;
import cn.exrick.xboot.modules.ApprovalInfo.service.IApprovalInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 处理审批超时
 */
@Component
@Slf4j
public class ApprovalTimeOut implements Runnable {


    @Override
    public void run() {
        try{
            //1.查询超时的审批数据并将超时的数据状态修改成超时
            ApprovalInfo approvalInfo=new ApprovalInfo();
            approvalInfo.setApprovalStatus(CommonConstant.APPROVAL_INIT);//3:初始值代表未审批状态
            IApprovalInfoService iApprovalInfoService=(IApprovalInfoService)SpringUtils.getBean("IApprovalInfoServiceImpl");
            List<ApprovalInfo> approvalInfos = iApprovalInfoService.selectApprovalInfoByAgo(approvalInfo);
            //2.将已经超时的数据状态修改成超时
            for (ApprovalInfo info : approvalInfos) {
                info.setApprovalStatus(CommonConstant.APPROVAL_TIMEOUT);
                iApprovalInfoService.saveOrUpdate(info);
                //审批数据超时后长连接websocket向前端发送超时状态返回


            }
            /*System.out.println(approvalInfos.size());
            System.out.println("线程测试");*/
        }catch (Exception e){
            log.info("超时数据出错");
            e.printStackTrace();
        }

    }
}
